/*
The MIT License (MIT)

Copyright (c) 2013 Mike Dapiran, Brian May, Richard Pospesel, and Bert Wierenga

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software 
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "ParameterField.h"

#include "Hogshead.Common.h"

namespace Hogshead
{
	namespace Core
	{
		const char* ParameterField::_type_names[13] = {"Unknown", "Bool", "Char", "Short", "Int", "Unsigned Int", "Float", "Double", "String", "Vector2", "Vector3", "Vector4", "Color"};

		ParameterField::ParameterField()
		{
			_data = new HashTable<Hogshead::Common::String, Pair<Type, void*>* >(2);
			_variable_names = new Vector<Hogshead::Common::String>(5, 15);
		}

		ParameterField::~ParameterField()
		{
			IEnumerator<Pair<Hogshead::Common::String, Pair<Type, void*> *> >& en = _data->getEnumerator();
			while(en.moveNext())
			{
				Pair<Hogshead::Common::String, Pair<Type, void*>* > & p = en.current();
				delete p.second;
			}
			delete _data;
			delete _variable_names;
		}

		void ParameterField::registerVariable(Type t, const Hogshead::Common::String& name, void* address)
		{
			if(_data->contains(name))
			{
				StringBuilder sb;
				sb << "Variable with name \"" << name << "\" already exists in this ParameterField object" << endl;
				throw Exception(sb);
			}

			Pair<Type, void*>* p = new Pair<Type, void*>(t, address);
			_data->add(name, p);
			_variable_names->add(name);
		}

		bool ParameterField::getType(const Hogshead::Common::String& name, Type& type) const
		{
			Pair<Type, void*>* p;
			if(_data->tryGetValue(name, p))
			{
				type = p->first;
				return true;
			}
			type = ParameterField::Unknown;
			return false;
		}

		bool ParameterField::getValue(const Hogshead::Common::String& name, void* address) const
		{
			Pair<Type, void*>* p;
			if(_data->tryGetValue(name, p))
			{
				switch(p->first)
				{
				case Bool:
					set<bool>(address, p->second);
					break;
				case Char:
					set<char>(address, p->second);
					break;
				case Short:
					set<short>(address, p->second);
					break;
				case Int:
					set<int>(address, p->second);
					break;
				case UnsignedInt:
					set<unsigned int>(address, p->second);
				case Float:
					set<float>(address, p->second);
					break;
				case Double:
					set<double>(address, p->second);
					break;
				case String:
					set<Hogshead::Common::String>(address, p->second);
					break;
				case Vector2:
					set<Hogshead::Common::Vector2>(address, p->second);
					break;
				case Vector3:
					set<Hogshead::Common::Vector3>(address, p->second);
					break;
				case Vector4:
					set<Hogshead::Common::Vector4>(address, p->second);
					break;
				case Color:
					set<Hogshead::Common::Color>(address, p->second);
					break;
				default:
					return false;
				}
				return true;
			}
			return false;
		}

		const Vector<String>& ParameterField::getVariableNames() const
		{
			return *_variable_names;
		}

		const char* ParameterField::getTypeName(Type t) const
		{
			return _type_names[(int)t];
		}

		void ParameterField::readXMLAttributes(TiXmlElement* node)
		{
			for(int k = 0; k < _variable_names->size(); k++)
			{
				Type t;
				const char* attribute = _variable_names->get(k);
				getType(attribute, t);
				
				switch(t)
				{
					case ParameterField::Bool:
						{
							bool b;
							if(node->QueryBoolAttribute(attribute, &b) == TIXML_SUCCESS)
								setValue<bool>(attribute, b);
							break;
						}
					case ParameterField::Short:
						{
							short s;
							if(node->QueryShortAttribute(attribute, &s) == TIXML_SUCCESS)
								setValue<short>(attribute, s);
							break;
						}
					case ParameterField::Int:
						{
							int i;
							if(node->QueryIntAttribute(attribute, &i) == TIXML_SUCCESS)
								setValue<int>(attribute, i);
							break;
						}
					case ParameterField::UnsignedInt:
						{
							unsigned int ui;
							if(node->QueryUnsignedIntAttribute(attribute, &ui) == TIXML_SUCCESS)
								setValue<unsigned int>(attribute, ui);
						}
					case ParameterField::Float:
						{
							float f = -1;
							const char* blah = node->Attribute(attribute);
							if(node->QueryFloatAttribute(attribute, &f) == TIXML_SUCCESS)
								setValue<float>(attribute, f);
							break;
						}
					case ParameterField::Double:
						{
							double d;
							if(node->QueryDoubleAttribute(attribute, &d) == TIXML_SUCCESS)
								setValue<double>(attribute, d);
							break;
						} 
					case ParameterField::String:
						{
							const char* s = node->Attribute(attribute);
							if(s)
								setValue<Hogshead::Common::String>(attribute, s);
							break;
						}
					case ParameterField::Color:
						{
							Hogshead::Common::Color c;
							if(node->QueryColorAttribute(attribute, &c) == TIXML_SUCCESS)
								setValue<Hogshead::Common::Color>(attribute, c);
							break;
						}
					case ParameterField::Vector2:
						{
							Hogshead::Common::Vector2 vec2;
							if(node->QueryVector2Attribute(attribute, &vec2) == TIXML_SUCCESS)
								setValue<Hogshead::Common::Vector2>(attribute, vec2);
							break;
						}
					case ParameterField::Vector3:
						{
							Hogshead::Common::Vector3 vec3;
							if(node->QueryVector3Attribute(attribute, &vec3) == TIXML_SUCCESS)
								setValue<Hogshead::Common::Vector3>(attribute, vec3);
							break;
						}
					case ParameterField::Vector4:
						{
							Hogshead::Common::Vector4 vec4;
							if(node->QueryVector4Attribute(attribute, &vec4) == TIXML_SUCCESS)
								setValue<Hogshead::Common::Vector4>(attribute, vec4);
							break;
						}
				}
			}
		}

		PrintStream& operator<<(PrintStream& in_stream, const ParameterField& pf)
		{
			const Vector<String>& names = pf.getVariableNames();
			bool b;
			char c;
			short s;
			int i;
			unsigned int ui;
			float f;
			double d;
			String st;
			Vector2 vec2;
			Vector3 vec3;
			Vector4 vec4;
			Color col;
			for(int k = 0; k < names.size(); k++)
			{
				ParameterField::Type t;
				if(pf.getType(names[k], t))
				{
					if(t == ParameterField::Unknown)
						continue;

					switch(t)
					{
					case ParameterField::Bool:
						in_stream << "bool ";
						pf.getValue(names[k], &b);
						in_stream << names[k] << " = " << b << ';' << endl;
						break;
					case ParameterField::Char:
						in_stream << "char ";
						pf.getValue(names[k], &c);
						in_stream << names[k] << " = " << c << ';' << endl;
						break;
					case ParameterField::Short:
						in_stream << "short ";
						pf.getValue(names[k], &s);
						in_stream << names[k] << " = " << s << ';' << endl;
						break;
					case ParameterField::Int:
						in_stream << "int ";
						pf.getValue(names[k], &i);
						in_stream << names[k] << " = " << i << ';' << endl;
						break;
					case ParameterField::UnsignedInt:
						in_stream << "unsigned int ";
						pf.getValue(names[k], &ui);
						in_stream << names[k] << " = " << ui << ';' << endl;
						break;
					case ParameterField::Float:
						in_stream << "float ";
						pf.getValue(names[k], &f);
						in_stream << names[k] << " = " << f << ';' << endl;
						break;
					case ParameterField::Double:
						in_stream << "double ";
						pf.getValue(names[k], &d);
						in_stream << names[k] << " = " << d << ';' << endl;
						break;
					case ParameterField::String:
						in_stream << "String ";
						pf.getValue(names[k], &st);
						in_stream << names[k] << " = \"" << st << "\";" << endl;
						break;
					case ParameterField::Vector2:
						in_stream << "Vector2 ";
						pf.getValue(names[k], &vec2);
						in_stream << names[k] << " = " << vec2 << ';' << endl;
						break;
					case ParameterField::Vector3:
						in_stream << "Vector3 ";
						pf.getValue(names[k], &vec3);
						in_stream << names[k] << " = " << vec3 << ';' << endl;
						break;
					case ParameterField::Vector4:
						in_stream << "Vector4 ";
						pf.getValue(names[k], &vec4);
						in_stream << names[k] << " = " << vec4 << ';' << endl;
						break;
					case ParameterField::Color:
						in_stream << "Color ";
						pf.getValue(names[k], &col);
						in_stream << names[k] << " = " << col << ';' << endl;
						break;
					}
				}
			}

			return in_stream;
		}
	}
}